package com.ktwlsoft.archivemanage.domain.business.businessService.study;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.AccountRepositoryService;
import com.ktwlsoft.archivemanage.domain.business.repositoryService.study.StudyCommentRepositoryService;
import com.ktwlsoft.archivemanage.domain.dao.entity.Account;
import com.ktwlsoft.archivemanage.domain.dao.entity.study.StudyComment;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.service.security.UserPrincipal;
import com.ktwlsoft.archivemanage.payload.study.studyClass.AddCommentRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author JokerMo
 * @version 1.0
 * @date 2019/12/25 17:40
 */
@Service
public class StudyCommentService {
    /**
     * 账号业务层接口实现类
     */
    private final AccountRepositoryService accountRepositoryService;
    /**
     * 课程评论
     */
    private final StudyCommentRepositoryService studyCommentRepositoryService;


    public StudyCommentService(AccountRepositoryService accountRepositoryService, StudyCommentRepositoryService studyCommentRepositoryService) {
        this.accountRepositoryService = accountRepositoryService;
        this.studyCommentRepositoryService = studyCommentRepositoryService;
    }

    /**
     * 添加评论
     *
     * @param request
     * @return Result
     */
    public Result<?> insert(AddCommentRequest request) {
        // 前端传入的值
        StudyComment studyComment = new StudyComment();
        studyComment.setClassId(request.getClassId());
        studyComment.setContent(request.getContent());
        //用户信息赋值
        Account account = accountRepositoryService.getById(request.getUserId());
        studyComment.setAccountId(account.getId());
        studyComment.setUserName(account.getUserName());
        studyComment.setUserRealName(account.getRealName());
        studyComment.setUserTelephone(account.getTeleNum());
        studyComment.setUserProfile(account.getProfile());
        try {
            studyCommentRepositoryService.insert(studyComment);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.SAVING_ERROR);
        }
        return Result.ok(studyComment.getId());
    }

    /**
     * 课程评论分页
     *
     * @param pn
     * @param pageSize
     * @param classId
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public Result<?> getClassComments(int pn, int pageSize, Long classId) {
        PageInfo<StudyComment> pageInfo = studyCommentRepositoryService.selectByClassId(pn, pageSize, classId);
        List<StudyComment> studyComments = studyCommentRepositoryService.getStudyCommentMapper().selectAmount(classId);
        int audited = studyComments.size();
        int unAudit = new Long(pageInfo.getTotal()).intValue() - audited;
        JSONObject object = new JSONObject();
        object.put("list", pageInfo);
        object.put("auditedAmount", audited);
        object.put("unAuditAmount", unAudit);
        return Result.ok(object);
    }

    /**
     * 评论审核
     *
     * @param id
     * @param passed
     * @return
     */
    public Result<?> auditComment(Long id, Boolean passed) {
        if (id == null) {
            return Result.body(null, ResultType.CHECK_ERROR).withMessage("评论id不能为空");
        } else {
            // 前端传入的值
            StudyComment studyComment = studyCommentRepositoryService.selectByPrimaryKey(id);
            studyComment.setPassed(passed);
            Account account = UserPrincipal.getCurrentUser();
            studyComment.setAuditor(account.getRealName());
            studyComment.setAuditorId(account.getId());
            studyComment.setAuditeAt(new Date());
            try {
                studyCommentRepositoryService.updateByPrimaryKey(studyComment);
            } catch (Exception e) {
                e.printStackTrace();
                return Result.body(null, ResultType.SAVING_ERROR);
            }
            return Result.ok(studyComment.getId());
        }
    }
}
